package org.gushi.datastruct.a03;

/**
 * 实现单项循环链表
 *
 * @param <E>
 */
public class CircleLinkedList<E> extends AbstractList<E> {
    //定义头节点
    private Node<E> first;

    private static class Node<E> {
        private E element;
        private Node<E> next;

        public Node(E element, Node<E> next) {
            this.element = element;
            this.next = next;
        }
        public boolean hasNext(){
            return next != null;
        }
    }


    @Override
    public void add(int index, E element) {
        if(index==0){
            first = new Node<>(element, first);
            //找到最后一个元素，指向最开始的元素
            Node<E> node = size==0?first:getNode(size-1);
            node.next = node;
        }else {
            Node<E> prev = getNode(index-1);
            prev.next = new Node<>(element, prev.next);
        }

        size++;
    }

    @Override
    public E remove(int index) {
        Node<E> old = first;
        if(index==0){// 如果是在第一个位置【index=0】删除元素，只需要指向你指向的下一个元素即可，如果你的下一个元素是指向第一个元素，代表此循环链表只有一个
            first = first.next;

            //找到最后一个元素，指向最开始的元素
            Node<E> node = size==0?first:getNode(size-1);
            node.next = node;

        }else {
            Node<E> prev = getNode(index-1);//拿到上一个元素，再指向你的下一个元素，如果你的下一个元素是第一个，代表是最后一个元素
            Node<E> self  = prev.next;
            Node<E> next  = self.next;
            prev.next = next;
        }
        size--;
        return old.element;
    }


    @Override
    public E get(int index) {
        Node<E> node = getNode(index);
        return node.element;
    }

    @Override
    public E set(int index, E element) {
        Node<E> node = getNode(index);
        Node<E> nodeOld = node;
        node.element = element;
        return nodeOld.element;
    }


    @Override
    public int indexOf(E element) {
        if(element==null){
            Node<E> node = first;
            for (int i = 0; i < size; i++) {
                if (node.element == null) {
                    return i;
                }
                node = node.next;
            }
        }else {
            Node<E> node = first;
            for (int i = 0; i < size; i++) {
                if (element.equals(node.element)) {
                    return i;
                }
                node = node.next;
            }
        }
        return ELEMENT_NOT_FOUND;
    }

    @Override
    public void clear() {
        size = 0;
        first = null;
    }

    private Node<E> getNode(int index){
        rangeCheck(index);
        Node<E> node = first;
        for (int i=0;i<index;i++){
            node = node.next;
        }
        return node;
    }

    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        string.append("size=").append(size).append(", [");
        Node<E> node = first;
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                string.append(", ");
            }

            string.append(node.element);

            node = node.next;
        }
        string.append("]");
        return string.toString();
    }
}
